package edu.uafs.museum;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

public class DisplayExhibitsActivity extends Activity {


	public static final String KEY_EXHIBITION = "exhibition";
	public static final String KEY_ID = "id";
	public static final String KEY_NAME = "name";
	public static final String KEY_IMAGE = "image";
	public static final String KEY_LINK = "link";
	public static final String KEY_DESCRIPTION = "description";
	
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exhibitions_layout);
		
		//Add code here to generate a list view of exhibits
		Intent intent = getIntent();
		String url = intent.getStringExtra(ExhibitsMain.EXHIBITS_XML_URL);
		
		//Create a new instance of the XMLParser class
		XMLParser parser = new XMLParser();
		
		//Call getxmlFromURL by passing the url of the xml file that is found on the internet
		//then returns a string representation to the xml file.
		String xml = parser.getxmlFromUrl(url);
		
		//Go through the xml string and create a Document object of it.
		Document doc = parser.getDomElement(xml);
		
		//Create a Node list of each Exhibition by looking through the Document
		//and returning a node list for each item that begins with the exhibition
		NodeList nl = doc.getElementsByTagName(KEY_EXHIBITION);
		
		//create an array list of exhibitions
		ArrayList<Exhibition> exhibitions = new ArrayList<Exhibition>();
		
		// looping through all exhibition nodes <exhibition>
	    for (int i = 0; i < nl.getLength(); i++) 
	    {
	    	Element e = (Element) nl.item(i);
	    	
	    	//create a new exhibition with the values from the xml file
	    	Exhibition exhibition = null;
			try {
				exhibition = new Exhibition(parser.getValue(e, KEY_ID), parser.getValue(e, KEY_NAME),
						parser.getValue(e, KEY_IMAGE), parser.getValue(e, KEY_LINK), parser.getValue(e, KEY_DESCRIPTION));
			} catch (IOException e1) {
				// if can't get the image from the url
				e1.printStackTrace();
			}

			// adding HashList to ArrayList
			exhibitions.add(exhibition);
		}
		
	    
	    
	    // Adding Exhibitions to ListView
	    ListAdapter adapter = new ExhibitionListAdapter(this, R.layout.exhibition_list_item, exhibitions);
	    
	    
	    
	 	// selecting single ListView item
	 	ListView lv = (ListView)findViewById(R.id.list);
	 	
	 	lv.setAdapter(adapter);
	 	
	 	lv.setOnItemClickListener(
	 		new OnItemClickListener() 
			{
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
				{
					// getting values from selected ListItem
					//get the text view has the link in the view
					TextView linkTV = (TextView)view.findViewById(R.id.link);
					
					//get the link string
					String link = linkTV.getText().toString();
					
					//get the text view has the id
					TextView idTV = (TextView)view.findViewById(R.id.id);
					
					//get the id
					String exId = idTV.getText().toString();

					// Starting new intent
					Intent in = new Intent(getApplicationContext(), DisplayExhibitActivity.class);
					in.putExtra(KEY_LINK, link);
					in.putExtra(KEY_ID, exId);

					startActivity(in);
				}
			}
	    );
	}
}
